package cn.edu.hunau.oop.day07;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author hb
 * @date 2025-08-12 09:53:34
 */
public class TestList {

    public void linkedListTest(){
        List<String> list1=new LinkedList<>();
    }

    @Test
    public void testInit(){
     List<String> list1=new ArrayList<>();
     list1.add("abc");
     list1.add(0,"def");
     list1.add("lucy");
     list1.add("lucy");
     System.out.println(list1);
     //List<String> list2 = List.of("abc", "def");
     //System.out.println(list2);
     //集合的遍历
        //1. 传统的for循环
        for(int i=0;i<list1.size();i++){
            System.out.print (list1.get(i)+" ");
        }
        System.out.println();

        //2. forEach循环
        for(String data:list1){
            System.out.print(data+" ");
        }
        System.out.println();

        //3. 迭代器
        Iterator<String> iter = list1.iterator();
        while(iter.hasNext()){
            String value=iter.next();
            System.out.print(value+" ");
        }
    }

    @Test
    public void testByColor(){
        List<Apple> datas=getApplies();
        //获得所有的红苹果
        List<Apple> redApples   =  FilterApple.filterByColor(datas,"红色");
        List<Apple> greenApples   = FilterApple.filterByColor(datas,"绿色");
        System.out.println(greenApples.size());
    }

    @Test
    public void test(){
        Supplier<Apple> s = ()-> new Apple("红心","黄色",134.5);
        Apple a=s.get();
        System.out.println(a);
    }

    @Test
    public void testFilter(){
        List<Apple> datas=getApplies();;

        datas.forEach(apple -> System.out.println(apple));
        //获得超过130g的红苹果
       //List<Apple> result= FilterApple.filter(datas,new Filter(){
       //     @Override
       //     public boolean filter(Apple apple) {
       //       return "红色".equals(apple.getColor()) && apple.getWeight()>130;
       //     }
       // });

        List<Apple> result= FilterApple.filter(datas,(apple)->"红色".equals(apple.getColor()) && apple.getWeight()>130);
        System.out.println(result);
        // 需要获得result中每个苹果的实际重量
        //List<Double> weights=new ArrayList<>();
        //for(Apple apple:result){
        //    weights.add(apple.getWeight());
        //}
        List<Double> weights = FilterApple.map(result, (apple) -> apple.getWeight());
        System.out.println(weights);


    }



    public List<Apple> getApplies(){
        List<Apple> appleList=new ArrayList<>();
        // 添加50个Apple对象
        appleList.add(new Apple("红富士", "红色", 112.7));
        appleList.add(new Apple("红富士", "红色", 98.5));
        appleList.add(new Apple("红富士", "红色", 156.3));
        appleList.add(new Apple("红富士", "红色", 189.1));
        appleList.add(new Apple("红富士", "红色", 132.4));
        appleList.add(new Apple("红富士", "绿色", 105.6));
        appleList.add(new Apple("红富士", "绿色", 176.2));
        appleList.add(new Apple("红富士", "绿色", 89.3));
        appleList.add(new Apple("红富士", "绿色", 145.8));
        appleList.add(new Apple("红富士", "青色", 167.9));
        appleList.add(new Apple("红富士", "青色", 122.5));
        appleList.add(new Apple("红富士", "青色", 93.7));
        appleList.add(new Apple("红富士", "青色", 192.3));
        appleList.add(new Apple("瑞雪", "红色", 101.2));
        appleList.add(new Apple("瑞雪", "红色", 155.8));
        appleList.add(new Apple("瑞雪", "红色", 138.4));
        appleList.add(new Apple("瑞雪", "红色", 85.6));
        appleList.add(new Apple("瑞雪", "绿色", 177.3));
        appleList.add(new Apple("瑞雪", "绿色", 112.9));
        appleList.add(new Apple("瑞雪", "绿色", 143.2));
        appleList.add(new Apple("瑞雪", "绿色", 97.8));
        appleList.add(new Apple("瑞雪", "青色", 188.5));
        appleList.add(new Apple("瑞雪", "青色", 126.4));
        appleList.add(new Apple("瑞雪", "青色", 109.1));
        appleList.add(new Apple("瑞雪", "青色", 163.7));
        appleList.add(new Apple("红星", "红色", 92.4));
        appleList.add(new Apple("红星", "红色", 151.6));
        appleList.add(new Apple("红星", "红色", 128.3));
        appleList.add(new Apple("红星", "红色", 179.8));
        appleList.add(new Apple("红星", "绿色", 103.5));
        appleList.add(new Apple("红星", "绿色", 147.2));
        appleList.add(new Apple("红星", "绿色", 88.7));
        appleList.add(new Apple("红星", "绿色", 166.9));
        appleList.add(new Apple("红星", "青色", 133.1));
        appleList.add(new Apple("红星", "青色", 115.4));
        appleList.add(new Apple("红星", "青色", 191.2));
        appleList.add(new Apple("红星", "青色", 99.6));
        appleList.add(new Apple("国光", "红色", 108.3));
        appleList.add(new Apple("国光", "红色", 159.7));
        appleList.add(new Apple("国光", "红色", 136.2));
        appleList.add(new Apple("国光", "红色", 87.5));
        appleList.add(new Apple("国光", "绿色", 172.4));
        appleList.add(new Apple("国光", "绿色", 121.8));
        appleList.add(new Apple("国光", "绿色", 149.3));
        appleList.add(new Apple("国光", "绿色", 95.9));
        appleList.add(new Apple("国光", "青色", 183.6));
        appleList.add(new Apple("国光", "青色", 118.7));
        appleList.add(new Apple("国光", "青色", 164.2));
        appleList.add(new Apple("国光", "青色", 104.5));

        return appleList;
    }
}
